//
// Copyright 2023 The GUAC Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"github.com/google/uuid"
	"github.com/guacsec/guac/pkg/assembler/graphql/model"
)

// VulnerabilityMetadata holds the schema definition for the VulnerabilityMetadata entity.
type VulnerabilityMetadata struct {
	ent.Schema
}

// Fields of the VulnerabilityMetadata.
func (VulnerabilityMetadata) Fields() []ent.Field {
	var scoreTypeValues []string
	for i := range model.AllVulnerabilityScoreType {
		scoreTypeValues = append(scoreTypeValues, model.AllVulnerabilityScoreType[i].String())
	}
	return []ent.Field{
		field.UUID("id", uuid.UUID{}).
			Default(getUUIDv7).
			Unique().
			Immutable(),
		field.UUID("vulnerability_id_id", getUUIDv7()),
		field.Enum("score_type").Values(scoreTypeValues...),
		field.Float("score_value"),
		field.Time("timestamp"),
		field.String("origin"),
		field.String("collector"),
		field.String("document_ref"),
	}
}

// Edges of the VulnerabilityMetadata.
func (VulnerabilityMetadata) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("vulnerability_id", VulnerabilityID.Type).Field("vulnerability_id_id").Required().Unique(),
	}
}

// Indexes of the VulnerabilityMetadata.
func (VulnerabilityMetadata) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("vulnerability_id_id", "score_type", "score_value", "timestamp", "origin", "collector", "document_ref").Unique(),
	}
}
